//
//  BTTabBarController.m
//  halfSweat
//
//  Created by 甘伟杰 on 16/5/12.
//  Copyright © 2016年 ganweijie. All rights reserved.
//

#import "BTTabBarController.h"
#import "HomeViewController.h"
#import "DiscoveryViewController.h"
#import "BTTbarBar.h"

@interface BTTabBarController ()

/** 模型数组 */
@property (strong, nonatomic) NSMutableArray<UITabBarItem *> *tabBarItems;

@end

@implementation BTTabBarController

#pragma mark - 懒加载
-(NSMutableArray<UITabBarItem *> *)tabBarItems{
    if (_tabBarItems == nil) {
        _tabBarItems = [NSMutableArray array];
    }
    return _tabBarItems;
}

#pragma mark - 视图生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化子控制器初始化子控制器
    [self setupChilidViewControllers];
    
    // 设置tabBar
    BTTbarBar *tabBar = [[BTTbarBar alloc] initWithFrame:self.tabBar.bounds];
    tabBar.tabBarItems = self.tabBarItems;
    tabBar.operationBlock = ^(NSInteger index){
        self.selectedIndex = index;
    };
    [self.tabBar addSubview:tabBar];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    for (UIView *subView in self.tabBar.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITabBarButton")])
        {
            [subView removeFromSuperview];
        }
    }
}

#pragma mark - 初始化子控制器
- (void) setupChilidViewControllers
{
    // 设置“首页”控制器
    HomeViewController *homeViewController = [[HomeViewController alloc] init];
    UINavigationController *homeNavigationController = [self setupChildViewController:homeViewController withNormalImage:[UIImage imageNamed:@"tab_icon_home"] selectedImage:[UIImage imageNamed:@"tab_icon_home_highlight"] tabBarItemTitle:@"首页"];
    [self addChildViewController:homeNavigationController];
    
    // 设置“发现”控制器
    DiscoveryViewController *discoveryViewController = [[DiscoveryViewController alloc] init];
    UINavigationController *discoveryNavigationController = [self setupChildViewController:discoveryViewController withNormalImage:[UIImage imageNamed:@"tab_icon_explore"] selectedImage:[UIImage imageNamed:@"tab_icon_explore_highlight"] tabBarItemTitle:@"发现"];
    [self addChildViewController:discoveryNavigationController];
    
    // 设置“商店”控制器
    UITableViewController *shopViewController = [[UITableViewController alloc] init];
    shopViewController.view.backgroundColor = [UIColor redColor];
    UINavigationController *shopNaviationController = [self setupChildViewController:shopViewController withNormalImage:[UIImage imageNamed:@"tab_icon_store"] selectedImage:[UIImage imageNamed:@"tab_icon_store_highlight"] tabBarItemTitle:@"商店"];
    [self addChildViewController:shopNaviationController];
    
    // 设置“关于我”控制器
    UITableViewController *profileViewController = [[UITableViewController alloc] init];
    UINavigationController *profileNavigationController = [self setupChildViewController:profileViewController withNormalImage:[UIImage imageNamed:@"tab_icon_me"] selectedImage:[UIImage imageNamed:@"tab_icon_me_highlight"] tabBarItemTitle:@"我"];
    [self addChildViewController:profileNavigationController];
    
}

- (UINavigationController *) setupChildViewController:(UIViewController *)viewConroller withNormalImage:(UIImage *)image selectedImage:(UIImage *)selectedImage tabBarItemTitle:(NSString *)title
{
    UINavigationController *NavigationController = [[UINavigationController alloc]initWithRootViewController:viewConroller];
    NavigationController.tabBarItem.image = image;
    NavigationController.tabBarItem.selectedImage = selectedImage;
    NavigationController.tabBarItem.title = title;
    
    [self.tabBarItems addObject:NavigationController.tabBarItem];   // 添加模型数组
    
    return NavigationController;
}

#pragma mark - 设置tabBar
- (void)setupTabBar {

}

@end
